<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Proxy对象</title>
</head>
<body>
  <script>
    /**
     * 1. Proxy对象可以重写对象上的属性
     * 2. 判断对象上是否存在某些属性
    */
    const person = { name: 'cs', age: 18 };
    const proxyInstance = new Proxy(person, {
      get (target, key) {
        // 在获取属性值之前会执行
        console.log('before get name', target, key);
        return target[key];
      },
      set (target, key, value) {
        target[key] = value;
        console.log('before set name', target, key, value);
      }
    });
    // 1. Proxy对象可以重写对象上的属性
    console.log(proxyInstance.name);
    proxyInstance.name = 'cs1'; // 重写对象上的属性
    console.log(proxyInstance.name);

    // 2. 判断对象上是否存在某些属性
    console.log(proxyInstance.hasOwnProperty('name'));
    console.log('name' in proxyInstance);
  </script>
</body>
</html>